<?php

namespace app\store\controller\leadership;

use app\store\controller\Controller;
use app\store\model\leader\Category as CategoryModel;
use app\store\model\Leader as LeaderModel;

/**
 * 领导管理
 * Class Leader
 * @package app\store\controller\store
 */
class Leader extends Controller
{
    /**
     * 领导列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $model = new LeaderModel;
        $list = $model->getList($this->request->get());
        return $this->fetch('index', compact('list'));
    }

    /**
     * 添加领导
     * @return array|bool|mixed
     * @throws \Exception
     */
    public function add()
    {
        $model = new LeaderModel;
        if (!$this->request->isAjax()) {
            $catgory = CategoryModel::getAll();
            return $this->fetch('add', compact('catgory'));
        }
        // 新增记录
        if ($model->add($this->postData('leader'))) {
            return $this->renderSuccess('添加成功', url('leader/index'));
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 编辑领导
     * @param $leader_id
     * @return array|bool|mixed
     * @throws \think\exception\DbException
     */
    public function edit($leader_id)
    {
        // 领导详情
        $model = LeaderModel::detail($leader_id);
        if (!$this->request->isAjax()) {
            $catgory = CategoryModel::getAll();
            return $this->fetch('edit', compact('model', 'catgory'));
        }
        // 新增记录
        if ($model->edit($this->postData('leader'))) {
            return $this->renderSuccess('更新成功', url('leader/index'));
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 删除领导
     * @param $leader_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function delete($leader_id)
    {
        // 领导详情
        $model = LeaderModel::detail($leader_id);
        if (!$model->setDelete()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

}